package com.jiutong.meipengdai.ui.activitys;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.base.CommonAdapter;
import com.jiutong.meipengdai.base.ViewHolder;
import com.jiutong.meipengdai.entity.HouseListRespond;
import com.jiutong.meipengdai.http.model.interfaces.CheckLoginInterface;
import com.jiutong.meipengdai.http.model.requests.HousessInfoRequest;
import com.jiutong.meipengdai.http.subscribers.ProgressSubscriber;
import com.jiutong.meipengdai.http.subscribers.SubscriberOnNextListener;
import com.jiutong.meipengdai.utils.NumUilt;

import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by suxi on 2017/6/2.
 */

@CheckLoginInterface
public class HouseListActivity extends BaseActivity {

    public static int HOUSE_RESULT_CORD = 10001;
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_no_date)
    TextView tvNoDate;
    @Bind(R.id.common_listview)
    ListView commonListview;
    private List<HouseListRespond.DataBean.HouseLoansBean> houseList;
    private boolean isFromLoanFragment; //是否从申请贷款界面过来

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_house_list;
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {
        super.initDate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            isFromLoanFragment = extras.getBoolean("isFromLoanFragment");
        }
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        tvTitle.setText("我的房产");

        commonListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                HouseListRespond.DataBean.HouseLoansBean houseLoansBean = houseList.get(position);
                Bundle bundle = new Bundle();

                if (!isFromLoanFragment) {
                    bundle.putString("houseId", houseLoansBean.getId());
                    openActivity(HouseDetailActivity.class, bundle);
                } else {
                    bundle.putParcelable("houseInfo", houseLoansBean);
                    Intent intent = new Intent();
                    intent.putExtras(bundle);
                    setResult(HOUSE_RESULT_CORD, intent);
                    finish();
                }
            }
        });
        getHouse();
    }

    @OnClick(R.id.rl_back)
    public void onViewClicked() {
        finish();
    }

    /**
     * 房产信息
     */
    public void getHouse() {

        HousessInfoRequest request = new HousessInfoRequest();
        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<HouseListRespond>() {
            @Override
            public void onNext(HouseListRespond subjects) {
                houseList = subjects.getData().getHouseLoans();

                if (houseList != null && houseList.size() > 0) {
                    tvNoDate.setVisibility(View.GONE);
                    HousessListAdapter housessListAdapter = new HousessListAdapter(mContext, houseList, R.layout.view_house_list_item);
                    commonListview.setAdapter(housessListAdapter);
                }
            }

            @Override
            public void onError(Throwable e) {

            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        request.getHouse(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }

    public class HousessListAdapter extends CommonAdapter<HouseListRespond.DataBean.HouseLoansBean> {

        public HousessListAdapter(Context context, List<HouseListRespond.DataBean.HouseLoansBean> listDatas, int layoutId) {
            super(context, listDatas, layoutId);
        }

        @Override
        protected void fillData(ViewHolder holder, int position) {
            HouseListRespond.DataBean.HouseLoansBean houseLoansBean = listDatas.get(position);
            TextView tvHouseNum = holder.getView(R.id.tv_house_num);
            TextView tvHouseAddress = holder.getView(R.id.tv_house_address);
            TextView tvPrice = holder.getView(R.id.tv_price);
            TextView tvUnit = holder.getView(R.id.tv_unit);

            tvHouseNum.setText(houseLoansBean.getCardNumber());
            tvHouseAddress.setText((houseLoansBean.getHouseProvince() + houseLoansBean.getHouseCity() + houseLoansBean.getHouseArea() + houseLoansBean.getHouseStreet() + houseLoansBean.getResidentialArea()).trim());
            String fainalAssAmount = houseLoansBean.getFainalAssAmount();

            if (TextUtils.isEmpty(fainalAssAmount))
                return;
            float price = Float.parseFloat(fainalAssAmount);

            if (price >= 10000) {
                tvPrice.setText(NumUilt.formatFloat(price / 10000) + "");
                tvUnit.setVisibility(View.VISIBLE);
            } else {
                tvPrice.setText(NumUilt.formatFloat(price) + "");
                tvUnit.setVisibility(View.GONE);
            }

        }
    }
}
